package com.cykj.admin.service.impl;

import com.cykj.admin.mapper.DictMapper;
import com.cykj.admin.service.IDictService;
import com.cykj.admin.vo.DictVO;
import com.cykj.common.dto.ResponseDTO;
import com.cykj.common.entity.CompanyCheckDO;
import com.cykj.common.entity.DictDataDO;
import com.cykj.common.entity.DictTypeDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: smart_recruit_backend
 * @BelongsPackage: com.cykj.admin.service.impl
 * @Author: 小小卢
 * @CreateTime: 2025-08-28 11:42
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class DictServiceImpl implements IDictService {

    @Autowired
    private DictMapper dictMapper;

    /**
    * @Description: 查询所有字典类型
    * @Param: []
    * @return: com.cykj.common.dto.ResponseDTO
    */
    @Override
    public ResponseDTO getAllDictTypeList() {
        List<DictTypeDO> allDictTypeList = dictMapper.getAllDictTypeList();
        return ResponseDTO.success(allDictTypeList);
    }

    /**
    * @Description: 条件查询字典类型
    * @Param: [dictVO]
    * @return: com.cykj.common.dto.ResponseDTO
    */
    @Override
    public ResponseDTO getDictTypeList(DictVO dictVO) {
        PageInfo<DictTypeDO> page;
        //开启分页
        PageHelper.startPage(dictVO.getCurrentPage(), dictVO.getPageSize());
        List<DictTypeDO> dictTypeList = dictMapper.getDictTypeList(dictVO);
        page = new PageInfo<>(dictTypeList);
        if (page.getTotal() < page.getPageSize() && dictTypeList.size() == 0 && page.getTotal() > 0) {
            dictVO.setCurrentPage(1);
            PageHelper.startPage(dictVO.getCurrentPage(), dictVO.getPageSize());
            List<DictTypeDO> dictTypeList2 = dictMapper.getDictTypeList(dictVO);
            page = new PageInfo<>(dictTypeList2);
        }
        return ResponseDTO.success(page);
    }

    /**
    * @Description: 新增字典类型
    * @Param: [dictVO]
    * @return: com.cykj.common.dto.ResponseDTO
    */
    @Override
    public ResponseDTO addDictType(DictVO dictVO) {
        Integer row = dictMapper.addDictType(dictVO);
        if (row > 0){
            return ResponseDTO.success(row);
        }
        return ResponseDTO.error("新增失败");
    }

    /**
    * @Description: 修改字典类型
    * @Param: [dictVO]
    * @return: com.cykj.common.dto.ResponseDTO
    */
    @Override
    public ResponseDTO updateDictType(DictVO dictVO) {
        Integer row = dictMapper.updateDictType(dictVO);
        if (row > 0){
            return ResponseDTO.success(row);
        }
        return ResponseDTO.error("修改失败");
    }

    /**
    * @Description: 启/禁用字典类型
    * @Param: [dictVO]
    * @return: com.cykj.common.dto.ResponseDTO
    */
    @Override
    public ResponseDTO deleteDictType(DictVO dictVO) {
        Integer row = dictMapper.deleteDictType(dictVO);
        if (row > 0){
            return ResponseDTO.success(row);
        }
        return ResponseDTO.error("修改失败");
    }

    /**
    * @Description: 查询字典数据
    * @Param: [dictVO]
    * @return: com.cykj.common.dto.ResponseDTO
    */
    @Override
    public ResponseDTO getDictDataList(DictVO dictVO) {
        PageInfo<DictDataDO> page;
        //开启分页
        PageHelper.startPage(dictVO.getCurrentPage(), dictVO.getPageSize());
        List<DictDataDO> dictDataList = dictMapper.getDictDataList(dictVO);
        page = new PageInfo<>(dictDataList);
        if (page.getTotal() < page.getPageSize() && dictDataList.size() == 0 && page.getTotal() > 0) {
            dictVO.setCurrentPage(1);
            PageHelper.startPage(dictVO.getCurrentPage(), dictVO.getPageSize());
            List<DictDataDO> dictDataList2 = dictMapper.getDictDataList(dictVO);
            page = new PageInfo<>(dictDataList2);
        }
        return ResponseDTO.success(page);
    }

    /**
     * @Description: 新增字典类型
     * @Param: [dictVO]
     * @return: com.cykj.common.dto.ResponseDTO
     */
    @Override
    public ResponseDTO addDictData(DictVO dictVO) {
        Integer row = dictMapper.addDictData(dictVO);
        if (row > 0){
            return ResponseDTO.success(row);
        }
        return ResponseDTO.error("新增失败");
    }

    /**
     * @Description: 修改字典数据
     * @Param: [dictVO]
     * @return: com.cykj.common.dto.ResponseDTO
     */
    @Override
    public ResponseDTO updateDictData(DictVO dictVO) {
        Integer row = dictMapper.updateDictData(dictVO);
        if (row > 0){
            return ResponseDTO.success(row);
        }
        return ResponseDTO.error("修改失败");
    }

    /**
     * @Description: 启/禁用字典数据
     * @Param: [dictVO]
     * @return: com.cykj.common.dto.ResponseDTO
     */
    @Override
    public ResponseDTO deleteDictData(DictVO dictVO) {
        Integer row = dictMapper.deleteDictData(dictVO);
        if (row > 0){
            return ResponseDTO.success(row);
        }
        return ResponseDTO.error("修改失败");
    }
}